package app.controller.msgmanage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import app.entity.UserSession;
import app.schedule.SpringApplicationContextHolder;
import app.service.VoiceAlertService;
import app.utils.Common;
import app.utils.DBDataGetterJDBCSiotwx;


/**
 * Servlet implementation class ErrorSpeech
 */
@WebServlet("/ErrorSpeech")
public class ErrorSpeech extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ErrorSpeech() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//response.setHeader("Content-Type", "application/json");
		//response.getWriter().append("{\"msg\":\"error\"}");
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserSession user = (UserSession) session.getAttribute(Common.sessionUser);
		List<HashMap<String, String>> errors=new ArrayList<>();
		Map<String, Object> error=new HashMap<>();
		error.put("can", true);
		error.put("num", "0");
		error.put("errors",new ArrayList<>());
		VoiceAlertService voiceAlertService= (VoiceAlertService) SpringApplicationContextHolder.context.getBean("VoiceAlertService");
		String userVoiceAlert = voiceAlertService.getUserVoiceAlert(user.getId());
		
		if (user==null||"0".equals(userVoiceAlert)) {
			error.put("can", false);
			String json = new Gson().toJson(error);
			response.setHeader("Content-Type", "application/json;charset=UTF-8");
			response.getWriter().write(json);
			return;
		}else {
			
			
			String sql = "SELECT facDesc FROM facmonitoring WHERE eventDevstu='0' and facRegionId="+user.getRegionId()+" ORDER BY eventStart DESC LIMIT 35";
			errors = DBDataGetterJDBCSiotwx.executeSqlWithResult(sql);
			List<String> errorsSpeech=new ArrayList<>();
			for(HashMap<String, String> entity:errors){
				errorsSpeech.add(entity.get("facDesc"));
			}
			sql="SELECT count(*) num FROM facmonitoring  WHERE eventEle!='' and facRegionId="+user.getRegionId()+" AND eventEle<'020'";
			errors = DBDataGetterJDBCSiotwx.executeSqlWithResult(sql);
			error.put("num", errors.get(0).get("num"));
			error.put("errors", errorsSpeech);
			
			String json = new Gson().toJson(error);
			response.setHeader("Content-Type", "application/json;charset=UTF-8");
			response.getWriter().write(json);
			return;
		}
	}
	
}
